﻿#include<iostream>
#include<vector>

using namespace std;

/*
总结const和指针的类型转化公式：
int* <= const int* 不可以的
const int* <= int* 可以的
*/
int sum(int x = 0, int y = 0)
{
    return x + y;
}
int sum2(int x=10, int y=20);
int sum2(int x, int y) { return x + y; }
//int sum3(int x, int y = 20);
//int sum3(int x = 10, int y);
int sum3(int x, int y) { return x + y; };
int sum4(int x, int y);
int sum4(int x = 10, int y = 20) { return x + y; }

#if 0
//int main()
//{
//
//    vector<int> niuniu; 
//    niuniu.push_back(0); 
//    niuniu.push_back(1); 
//    niuniu.push_back(2); 
//    for (vector<int>::size_type i = niuniu.size() - 1; i >= 0; i--)
//    {   
//        cout << i << endl;
//        cout << niuniu[i] << endl;
//    }
//    return 0;
//    //sum2();
//    //sum3();
//    //sum4();
//
//    /*
//    int a = 0;
//    int b = 10;
//    int ret = sum(a, b);
//    */
//
//    /*
//    mov ecx, dword ptr[ebp-8] //先将b移动入栈
//    push ecx
//    mov ecx, dword ptr[ebp-4]
//    push ecx 
//    call sum
//    */
//
//    // cout << ret << endl;
//
//    // ret = sum(a);
//    /* 
//    push 0 //b默认为0
//    mov ecx, dword ptr[ebp-4]
//    push ecx
//    call sum
//    */
//
//    // ret = sum(); // 等价于sum(0,10);因为数字直接存放再内存中，所以直接可以调用。
//    /*
//    push 0 //b默认为0
//    push 0
//    call sum
//    */
//    return 0;
//}
#endif

/*
int a = 10;
int* p = &a;
const int*& q = p; //const int** q=&p=int** p;错误

int a = 10;
const int*  p = &a;
int*& q = p;//int** q=const int** p;错误

int a = 10;
int* const p = &a;
int*& q = p; //int** q=int* const* p; 错误
//或者 int**q=&p;p是const修饰的量，不能将其地址赋值给普通变量

int a = 10;
int* p = &a;
int*& q = p; //int** p=&q;
*/

/*
//写一句代码，在内存的0x0018ff4处写一个为4字节的整数10；
int* p = (int*)0x0018ff4;
*p = 10;

int a = 10;
int* p = &a;
int** q = &p;
int*& m = p; // int** m=&p;还原回来就是这样的

//判断是否正确
//const int*& k = p; // const int** k=&p; const int** <- int**;所以是错误的
*/
/*
    int a = 10;
    const int* p = &a; // const int* <- int*
    int* const* q = &p; // int* const* <- const int **;等价于int* <- const int*。错误
    */

    /*
    int a = 10;
    int*const p = &a; // int* <- int*;
    int** q = &p;//int** <- int* const*;错误，因为p取了地址，所以需要考虑const
    */

    /*
    int a = 10;
    int* p = &a; // int* <- int*;
    int** const q = &p;//int** <- int**;
    */

    /*
    int a = 10;
    int* p = &a; // int* <- int*;
    int* const* q = &p;//int* const* <- int**;
    */

    /*
    int a = 10;
    int* p = &a; // int* <- int*;
    const int** q = &p; // const int ** <- int**; 错误，不支持转化
    */

    /*
    int a = 10;
    int* const p = &a; // int* <- int*;
    const int* q = p; // const int* <- int*
    */

    /*
    int a = 10;
    int* const p = &a; //int* <- int*; const后面没有*所以不参与类型转化
    int* const q = p; //int* <- int*
    */

    /*
    int a = 10;
    int* const p = &a; //int* <- int*; const后面没有*所以不参与类型转化
    int* q = p; //int* <- int*
    */

/*
int a = 10;
const int* p = &a; //const int* <- int *;
int* q = p; //int* <- const int*;错误
*/

/*
//const和二级指针
int a = 10;
int* p = &a;
int** q = &p;
// const int** m = &p; // const int ** <= int **
*/

/*
//const和一级指针
int* q1 = nullptr;
int* const q2 = nullptr; // const右边没有*，则不参与类型
// int*,int *
cout << typeid(q1).name() << "," << typeid(q2).name() << endl;

int a = 10;
int* p1 = &a;
const int* p2 = &a; // const int* <- int*
int* const p3 = &a; // int*       <- int*
int* p4 = p3;       // int*       <- int*
*/

/*
const int a = 10;
// int *p1 = &a;会出现问题 通过指针修改a的值 *p=30;int* <- const int*
int* p1 = (int*)&a; //通过类型转化，将const int*转化为int*
*p1 = 30;
cout << a << "," << *p1 << "," << *(&a) << endl; //10,30,30

const int* p2 = &a; // 不能通过指针修改其对应的值 *p2=20;
*/